<?php

/**
 * reservaderecurso actions.
 *
 * @package    ecampus
 * @subpackage reservaderecurso
 * @author     Grupo 608
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reservaderecursoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->reservaderecursos = Doctrine::getTable('Reservaderecurso')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $this->id_recurso = $request->getParameter('id_recurso');
    $this->id_curso = $request->getParameter('id_curso');
    
    $this->form = new ReservaderecursoForm();
    if(!is_null($this->id_recurso))
    {
        $this->form->setRecurso($this->id_recurso);
    }
    if( !is_null($this->id_curso) )
    {
        $this->form->setCurso($this->id_curso);
    }
    if(is_null($this->id_curso) && !is_null($this->id_usuario))
    {
        $this->form->setCursos($this->id_usuario);
    }

  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $this->id_recurso = $request->getParameter('id_recurso');
    $this->id_curso = $request->getParameter('id_curso');
    

    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ReservaderecursoForm();

    $this->processForm($request, $this->form);

//    $this->id_usuario = $this->form->getObject()->getCurso()->getDocente()->getIdUsuario();

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($reservaderecurso = Doctrine::getTable('Reservaderecurso')->find(array($request->getParameter('id_codigo_reserva'))), sprintf('Object reservaderecurso does not exist (%s).', $request->getParameter('id_codigo_reserva')));
    $this->form = new ReservaderecursoForm($reservaderecurso);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($reservaderecurso = Doctrine::getTable('Reservaderecurso')->find(array($request->getParameter('id_codigo_reserva'))), sprintf('Object reservaderecurso does not exist (%s).', $request->getParameter('id_codigo_reserva')));
    $this->form = new ReservaderecursoForm($reservaderecurso);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($reservaderecurso = Doctrine::getTable('Reservaderecurso')->find(array($request->getParameter('id_codigo_reserva'))), sprintf('Object reservaderecurso does not exist (%s).', $request->getParameter('id_codigo_reserva')));
    $reservaderecurso->delete();

    $this->redirect('reservaderecurso/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $reservaderecurso = $form->save();

      $id_usuario = $request->getParameter('id_usuario');
      $this->redirect('docente/index?id_usuario='.$id_usuario);
//      $this->redirect('reservaderecurso/edit?id_codigo_reserva='.$reservaderecurso->getIdCodigoReserva());
    }
  }
}
